package controller;

import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author AgusNH
 */
public class Controller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, Exception {
        String page = request.getParameter("page");
        RequestDispatcher dis = null;
        if (page != null) {
            if (page.equals("registered")) {
                LogicAkun reg = new LogicAkun(request);
                dis = request.getRequestDispatcher(reg.register());
            } else if (page.equals("register")) {
                dis = request.getRequestDispatcher("register.jsp");
            } else if (page.equals("list")) {
                LogicAkun hapus = new LogicAkun(request);
                dis = request.getRequestDispatcher(hapus.deleteUser());
            } else if (page.equals("edit")) {
                LogicAkun edit = new LogicAkun(request);
                dis = request.getRequestDispatcher(edit.goEditProfil());
            } else if (page.equals("edited")) {
                LogicAkun edit = new LogicAkun(request);
                dis = request.getRequestDispatcher(edit.updateProfil());
            } else if (page.equals("login")) {
                LogicAkun login = new LogicAkun(request);
                dis = request.getRequestDispatcher(login.goProfil());
            } else if (page.equals("loggedin")) {
                LogicAkun login = new LogicAkun(request);
                dis = request.getRequestDispatcher(login.login());
            } else if (page.equals("profil")) {
                LogicAkun login = new LogicAkun(request);
                dis = request.getRequestDispatcher(login.goProfil());
            } else if (page.equals("logout")) {
                LogicAkun logout = new LogicAkun(request);
                dis = request.getRequestDispatcher(logout.logout());
            } 
              //kontroller BARANG
              else if (page.equals("listbarang")) {
                LogicBarang list = new LogicBarang(request);
                dis = request.getRequestDispatcher(list.listBarang());
            } else if (page.equals("form_addbarang")) {
                dis = request.getRequestDispatcher("form.jsp");
            } else if (page.equals("addbarang")) {
                LogicBarang add = new LogicBarang(request);
                dis = request.getRequestDispatcher(add.add());
            } else if (page.equals("deletebarang")) {
                LogicBarang delete = new LogicBarang(request);
                dis = request.getRequestDispatcher(delete.delete());
            } else if (page.equals("formupdatebarang")) {
                LogicBarang selectone = new LogicBarang(request);
                dis = request.getRequestDispatcher(selectone.selectone());
            } else if (page.equals("updatebarang")) {
                LogicBarang update = new LogicBarang(request);
                dis = request.getRequestDispatcher(update.update());
            }
        } else {
            dis = request.getRequestDispatcher("index.jsp");
        }
        dis.forward(request, response);
    }     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
